package com.liujit.bms.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.app.domain.model.BlogArticle;
import com.liujit.bms.app.domain.model.BlogMemberCollection;
import com.liujit.bms.app.domain.query.BlogMemberCollectionQuery;
import com.liujit.bms.app.domain.result.BlogMemberCollectionResult;
import com.liujit.bms.app.mapper.BlogMemberCollectionMapper;
import com.liujit.bms.app.service.BlogArticleService;
import com.liujit.bms.app.service.BlogMemberCollectionService;
import com.liujit.common.constants.ColumnConstant;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.Pagination;
import com.liujit.web.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description 博客会员收藏Service实现类
 * @Author liujun
 * @Date 2021-07-16 18:15:42
 */
@RequiredArgsConstructor
@Service
public class BlogMemberCollectionServiceImpl extends ServiceImpl<BlogMemberCollectionMapper, BlogMemberCollection>
        implements BlogMemberCollectionService {

    private final BlogArticleService blogArticleService;

    @Override
    public PageBean<BlogMemberCollectionResult> getPage(BlogMemberCollectionQuery query) {
        query.setSortName(ColumnConstant.COLLECTION_TIME);
        query.setSortOrder(SqlKeyword.DESC.getSqlSegment());
        Page<BlogMemberCollection> page = Pagination.page(query);
        LambdaQueryWrapper<BlogMemberCollection> wrapper = new LambdaQueryWrapper();
        IPage<BlogMemberCollection> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, BlogMemberCollectionResult.class);
    }

    @Override
    public Boolean add(BaseParams params) {
        BlogArticle blogArticle = blogArticleService.getById(params.getId());
        if (blogArticle == null) {
            throw new DataNotFoundException();
        }
        // 添加收藏量
        super.baseMapper.addLikeNum(params.getId());
        BlogMemberCollection entity = new BlogMemberCollection();
        entity.setMemberId(JwtUtil.getUserId());
        entity.setArticleId(blogArticle.getId());
        entity.setTitle(blogArticle.getTitle());
        entity.setCoverImage(blogArticle.getCoverImage());
        entity.setCollectionTime(new Date());
        // 添加
        return super.save(entity);
    }

    @Override
    public Boolean modify(BaseParams params) {
        // 减少收藏量
        super.baseMapper.reduceLikeNum(params.getId());
        LambdaQueryWrapper<BlogMemberCollection> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberCollection::getArticleId, params.getId());
        wrapper.eq(BlogMemberCollection::getMemberId, JwtUtil.getUserId());
        return super.remove(wrapper);
    }

    @Override
    public Integer get(Long id) {
        LambdaQueryWrapper<BlogMemberCollection> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberCollection::getArticleId, id);
        wrapper.eq(BlogMemberCollection::getMemberId, JwtUtil.getUserId());
        return super.count(wrapper);
    }

    @Override
    public Boolean del(BaseParams params) {
        BlogMemberCollection blogMemberCollection = super.getById(params.getId());
        if (blogMemberCollection == null) {
            throw new DataNotFoundException();
        }
        // 减少收藏量
        super.baseMapper.reduceLikeNum(blogMemberCollection.getArticleId());
        LambdaQueryWrapper<BlogMemberCollection> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberCollection::getId, params.getId());
        wrapper.eq(BlogMemberCollection::getMemberId, JwtUtil.getUserId());
        return super.removeById(params.getId());
    }

    @Override
    public Boolean clear() {
        Long userId = JwtUtil.getUserId();
        // 减少收藏量
        super.baseMapper.reduceLikeNumByMemberId(userId);
        LambdaQueryWrapper<BlogMemberCollection> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberCollection::getMemberId, userId);
        return super.remove(wrapper);
    }
}
